package ipdesk.model.report.consult.action;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import ipdesk.model.report.brand.dao.BrandInfoDao;
import ipdesk.model.report.consult.bean.ConsultInfoBean;
import ipdesk.model.report.consult.dao.ConsultInfoDao;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ecg.itf.dao.ECGDao;
import ecg.util.FileService;
import ecg.util.SqlMapLocator;
import ecg.util.StringUtils;
import ecg.util.Time;

public class ConsultInfoRegAction extends ActionSupport implements Preparable, ModelDriven<ConsultInfoBean>
{
	
	private ConsultInfoBean consultInfoBean;
	FileService fileService = new FileService();
	private ConsultInfoBean consultUploadBean;
	

	// 상세보기 뷰페이지에서 사용되는 도메인 객체
	private ConsultInfoBean consultInfoDetail;
	
	private int ConsultingSeq;
	/**
	 * 지재권 상담일지 등록 액션 처리
	 */
	public String execute() throws Exception
	{
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.consult");
		String totalPath = uplodaPathPrefix + basePath;
		
		String regTime = Time.getDateTime();
			
		consultInfoBean.setPathName(basePath);
		consultInfoBean.setRegTime(regTime);
		consultInfoBean.setDifficulty(consultInfoBean.getDifficulty());
		consultInfoBean.setConsultingContent(consultInfoBean.getConsultingContent());
		consultInfoBean.setFuturePlan(consultInfoBean.getFuturePlan());
		
		
	

		// 상담정보 insert.
		int lastConsultingSeq = ConsultInfoDao.getInstance().consultInfoReg(consultInfoBean);
	
		// 마지막으로 insert된 consult_seq 값을 빈 객체에 저장.		
		consultInfoBean.setConsultingSeq(lastConsultingSeq);
	
		if(lastConsultingSeq != 0 && consultInfoBean.getUpload() != null)
		{
			String uploadFileName = consultInfoBean.getUploadFileName();
			String realFileName = fileService.saveFile(consultInfoBean.getUpload(), totalPath, uploadFileName);
			consultInfoBean.setUploadFileName(realFileName);
			ConsultInfoDao.getInstance().insertFileData(consultInfoBean);
		}
	
		return SUCCESS;
	}
	
	/**
	 * 지재권 상담일지 수정 폼 액션
	 */
	public String executeModifyForm() throws Exception
	{
		/**
		 * 넘어온 컨설팅 번호를 조건으로 DB에서 데이터를 가져온다.
		 */
		int consultSeq = consultInfoBean.getConsultingSeq();
		
		consultInfoDetail = ConsultInfoDao.getInstance().getModifyData(consultSeq);
		
		return SUCCESS;
	}
	
	/**
	 * 지재권 상담일지 수정 액션 처리
	 */
	public String executeModify() throws Exception
	{
		/**
		 * 	1. 넘어온 컨설팅 번호를 조건으로 업로드된 데이터가 있는지 여부를 확인한다.
		 */
		int consultingSeq = consultInfoBean.getConsultingSeq();
		
		consultUploadBean = ConsultInfoDao.getInstance().getUploadData(consultingSeq);

		
		// 데이터 업데이트, 파일 업로드 처리.
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.consult");
		String totalPath = uplodaPathPrefix + basePath;
		
		String regTime = Time.getDateTime();
		String modifyTime = Time.getDateTime();

		consultInfoBean.setPathName(basePath);

		if(consultInfoBean.getUpload()!=null){
			String uploadFileName = consultInfoBean.getUploadFileName();
			String realFileName = fileService.saveFile(consultInfoBean.getUpload(), totalPath, uploadFileName);
			consultInfoBean.setUploadFileName(realFileName);
			ConsultInfoDao.getInstance().insertFileData(consultInfoBean);	
		}

		consultInfoBean.setPathName(basePath);
		
		consultInfoBean.setModifyTime(modifyTime);
		
		
		// 상담정보 update
		int result = ConsultInfoDao.getInstance().consultInfoUpdate(consultInfoBean);
	
		return SUCCESS;
	}
	
	
	/**
	 * 데이터 삭제 액션
	 * 
	 */
	public String executeDelete() throws Exception
	{
		/**
		 * 1. 해당 consultingSeq의 path와 file name을 가지고 온다.
		 * 2. 테이블의 파일 데이터를 지운다.
		 * 3. consult 데이터를 지운다.
		 * 4. 업로드 데이터를 지운다.
		 */
		
		boolean delFlag = ConsultInfoDao.getInstance().consultInfoDelete(consultInfoBean.getConsultingSeq());
		
		return SUCCESS;
	}
	
	/**
	 * 파일 삭제 액션
	 * @return
	 */
	public String executeFileDelete() throws Exception
	{
		System.out.println("***메소드 들어옴");
		//ConsultInfoDao.getInstance().consultInfoUpdateFile(consultInfoBean);
		ConsultInfoDao.getInstance().fileDelete(consultInfoBean.getConsultingSeq());
		
		String filePathPrefix = getText("path.upload.prefix");
		String fileName = consultInfoBean.getFileName();
		String pathName = consultInfoBean.getPathName();
									
		fileService.deleteFile(filePathPrefix + pathName + fileName);

		return SUCCESS;
	}
	
	public ConsultInfoBean getConsultInfoDetail()
	{
		return consultInfoDetail;
	}

	public void setConsultInfoDetail(ConsultInfoBean consultInfoDetail)
	{
		this.consultInfoDetail = consultInfoDetail;
	}
	public void prepare() throws Exception
	{
		consultInfoBean = new ConsultInfoBean();
	}
	
	public int getConsultingSeq()
	{
		return ConsultingSeq;
	}

	public void setConsultingSeq(int consultingSeq)
	{
		ConsultingSeq = consultingSeq;
	}

	public ConsultInfoBean getModel()
	{
		return consultInfoBean;
	}
	
	public ConsultInfoBean getConsultUploadBean() {
		return consultUploadBean;
	}

	public void setConsultUploadBean(ConsultInfoBean consultUploadBean) {
		this.consultUploadBean = consultUploadBean;
	}
	
	/*public void validate()
	{
		for(int i=0; i<consultInfoBean.getUploadContentType().size(); i++){
			String contentType = consultInfoBean.getUploadContentType().get(i);
			long fileSize = 0;
			
			if(consultInfoBean.getUpload() != null)
			{
				fileSize = consultInfoBean.getUpload().size();
			}
			
			if(!fileService.contentTypeValidate(contentType) && !fileService.contentTypeValidate2(contentType))
			{
				addFieldError("upload", "업로드할 수 없는 파일형식입니다");
			}
			
			if(fileSize > Long.parseLong(getText("upload.size")))
			{
				addFieldError("upload", "업로드 용량을 초과하였습니다.");
				return;
			}
		}
	}*/
}
